package com.lmind;

import java.net.InetSocketAddress;
import java.util.concurrent.ScheduledThreadPoolExecutor;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.lmind.core.gis.Location;
import com.lmind.core.gps.alone.SmartphoneCodec;
import com.lmind.core.gps.terminal.AbstractGatewayConnector;
import com.lmind.core.gps.terminal.message.LocationMessage;

public class AndroidActivity extends Activity {
	
	private SmartphoneCodec codec;
	
	private final AbstractGatewayConnector cc = new AbstractGatewayConnector();
	
	private Button button;
	
	private EditText lon;
	
	private EditText lat;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
    	System.setProperty("java.net.preferIPv6Addresses", "false");
    	
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.main);
        
        button = (Button)findViewById(R.id.button1);
        lon = (EditText)findViewById(R.id.lon);
        lat = (EditText)findViewById(R.id.lat);
        
        
        button.setOnClickListener(new ButtonClicker());
        
        codec = new SmartphoneCodec();

        cc.setDeviceSerial("13123455555");
        cc.setExecutorService(new ScheduledThreadPoolExecutor(1));
        cc.setMessageCodec(codec);
        
        cc.connect(new InetSocketAddress("192.168.2.7", 15110));
        
    }
    
    
    class ButtonClicker implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			String a = lon.getText().toString();
			String b = lat.getText().toString();
			
			double c = Double.parseDouble(a);
			double d = Double.parseDouble(b);
			
			cc.sendMessage(new LocationMessage(new Location(d, c)));
		}
    	
    }
}